package org.lex.explorer.model;

import java.io.File;

import javax.swing.filechooser.FileView;

import org.lex.catalog.AbstractCatalogModel;
import org.lex.io.filesystem.LocalFileView;

public class DefaultFileSystemModel extends AbstractCatalogModel<File> implements FileSystemModel {
	private static final File[] EMPTY = new File[0];

	private File dir = null;
	private File[] children = EMPTY;
	private FileView view = null;

	public DefaultFileSystemModel(File dir) {
		this.dir = dir;
		this.view = new LocalFileView();
	}

	public FileView getFileView() {
		return view;
	}

	public void setFileView(FileView view) {
		if (null != view) {
			this.view = view;
			this.fireCatalogChanged(this.dir);
		}
	}

	public File getCatalog() {
		return dir;
	}

	public File[] getChildren() {
		return children;
	}

	public void setCatalog(File dir) {
		this.changeDirectory(dir);
	}

	private void changeDirectory(File directory) {
		if (null == directory || false == directory.exists() || false == directory.isDirectory())
			return;
		this.children = directory.listFiles();
		if (null == children)
			children = EMPTY;
		this.dir = directory;
		this.fireCatalogChanged(directory);
	}
}
